package pratice.fengzhongye.interview;

import org.junit.Test;
import sun.applet.Main;

import java.lang.reflect.Field;

/**
 * @author 吴宇亮 on 2019/2/18 0018 16:18
 */
public class Lesson3_private {

    @Test
    public void test() throws NoSuchFieldException, IllegalAccessException {
        PrivateTest test = new PrivateTest();
        System.out.println(test.getName());

        Class<?> clazz = PrivateTest.class;
        Field name = clazz.getDeclaredField("name");
        name.setAccessible(true);
        name.set(test, "world");

        System.out.println(test.getName());
    }
}

/**
 * 如何用反射改变name的值
 */
class PrivateTest{
    private String name = "hello";

    public String getName(){
        return name;
    }
}
